package com.gisuni.sddc.platformManager.controller;

import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.GET;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import com.gisuni.sddc.platformManager.model.Organization;
import com.gisuni.sddc.platformManager.model.Response;
import com.gisuni.sddc.platformManager.model.Tree;
import com.gisuni.sddc.platformManager.service.OrganizationService;
import com.gisuni.sddc.util.StringUtil;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

@IocBean(singleton = false)
@At("/client/backgroundManagement/organizationController")
public class OrganizationController  {

	private Log log = Logs.getLog(UserController.class);
	
	@Inject
    private OrganizationService organizationService;
	

	@At("/tree")
	@GET
	@POST
	@Ok("json")
    public Object tree(HttpServletRequest request) {
		String state  = request.getParameter("state");
		String type  = request.getParameter("type");
		List<Tree>  trees = organizationService.findTree(type);

		if("open".equals(state)){
			for(Tree e : trees){
				e.setState("open");
			}
		}
		return trees;
    }

	@At("/treeGrid")
	@GET
	@POST
	@Ok("raw:json") 
    public Object treeGrid() {
		//TODO 前端如何做到节点收缩而不发送请求到后台呢 
        String t  = organizationService.findTreeGrid();
         return t;
    }

	@At("/add")
	@GET
	@POST
	@Ok("json")
    public Object add(HttpServletRequest request) {
		//TODO 在前端 选择负责人时,名称如果有重复的,下拉框的显示值可加上userCode
		Response res = new Response();
		res.setSuccess(false);
		try {
			res =  organizationService.addOrganization(request);//返回格式:{}
		} catch (Exception e) {
			log.error(e.getMessage());
			return res;
		}
		return res;
    }

	@At("/editPage")
	@GET
	@POST
	@Ok("jsp:/client/backgroundManagement/views/admin/organizationEdit.jsp")
    public String editPage(HttpServletRequest request, String id, String hechaTarget, String qiatanTarget) {
        Organization organization = organizationService.findOrganizationById(id);
        if(!StringUtil.isNullOrEmpty(hechaTarget)){
        	organization.setHechaTarget(hechaTarget);
        }
        if(!StringUtil.isNullOrEmpty(qiatanTarget)){
        	organization.setQiatanTarget(qiatanTarget);
        }
        request.setAttribute("organization", organization);
//        return "admin/organizationEdit";
        return null;
    }

	@At("/edit")
	@GET
	@POST
	@Ok("json")
    public Object edit(HttpServletRequest request) {
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
			res = organizationService.updateOrganization(request);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return res;
    }

	@At("/delete")
	@GET
	@POST
	@Ok("json")
    public Object delete(String id) {
		Response res = new Response();
		res.setSuccess(false);
		try {
			res =  organizationService.deleteOrganizationById(id);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return res;
    }
}
